import { request } from "../utils"
import { Toast } from "vant"

/*
  获取品牌文章
*/
const getBrand = (requestData, cb) => {
  request({
    url: "api/Article/GetArticleList",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let list = res.data.Data.rows
      list = list.map(item => {
        item.Articles = item.Articles.filter((item) => item.isShow)
        return item
      })
      cb && cb(list)
    } else {
      console.log("获取文章失败...")
    }
  })
}

/*
  获取品牌文章详情
*/
const getBrandDetail = (requestData, cb) => {
  request({
    url: "api/Article/GetArticle",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取文章详情失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  收藏文章
*/
const collectBrandArticle = (requestData, cb) => {
  request({
    method: 'POST',
    url: "api/Article/PostAddFavoriteArticle",
    data: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Msg)
    } else {
      console.log("收藏文章失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取文章评论
*/
const getArticleComment = (requestData, cb) => {
  request({
    url: "api/Article/GetArticleComment",
    params: requestData,
  }).then((res) => {
    if (res.data.Success) {
      let list = res.data.Data.reverse()
      cb && cb(list)
    } else {
      console.log("获取文章评论失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  文章评论
*/
const insertArticleComment = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/Article/AddArticeComment",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb(res.data.Msg)
    } else {
      console.log("文章评论失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  文章点赞
*/
const likeArticle = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/Article/PostArticleZan",
    data: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb(res.data.Msg)
    } else {
      console.log("文章点赞失败...")
      Toast(res.data.Msg)
    }
  })
}

export {
  getBrand,
  getBrandDetail,
  collectBrandArticle,
  getArticleComment,
  insertArticleComment,
  likeArticle,
}
